package jdbc;

import org.junit.Test;
import utils.JdbcUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class BatchDemo {

    @Test
    public void addBatch(){
        Connection conn = null;
        PreparedStatement ps = null;
        String sql = "insert into user(username, birthday, sex, address) values(?, '1992-05-03', 'w', 'sichuan')";

        try {
            conn = JdbcUtils.getConnection();
            ps = conn.prepareStatement(sql);
            for (int i=0; i<4; i++) {
                ps.setString(1, "crifan"+i);
                ps.addBatch();
            }
            int[] is = ps.executeBatch();

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JdbcUtils.free(ps, conn, null);
        }

    }
}
